package ui.troopmanager.mooresr.us;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import troopmanager.mooresr.us.DataManager;
import troopmanager.mooresr.us.Person;
import troopmanager.mooresr.us.Vehicle;

public class VehicleDetailDialog extends JDialog {
	private static final long serialVersionUID = 1L;

	private JLabel yearLabel = new JLabel("Year:");
	private JTextField yearText = new JTextField(5);
	private JLabel makeLabel = new JLabel("Make:");
	private JTextField makeText = new JTextField(20);
	private JLabel modelLabel = new JLabel("Model:");
	private JTextField modelText = new JTextField(20);
	private JLabel seatbeltsLabel = new JLabel("Seatbelts:");
	private JTextField seatbeltsText = new JTextField(4);
	private JLabel licensePlateLabel = new JLabel("License Plate:");
	private JTextField licensePlateText = new JTextField(10);
	private JLabel liabPerPersonLabel = new JLabel("Liability Per Person:");
	private JTextField liabPerPersonText = new JTextField(7);
	private JLabel liabPerAccidentLabel = new JLabel("Liability Per Accident:");
	private JTextField liabPerAccidentText = new JTextField(7);
	private JLabel propertyDamageLabel = new JLabel("Property Damage:");
	private JTextField propertyDamageText = new JTextField(7);
	private JLabel ownerLabel = new JLabel("Owner:");
	private JComboBox ownerComboBox = new JComboBox();
	private JButton okButton = new JButton("OK");
	private JButton cancelButton = new JButton("Cancel");
	
	private Integer vehicleId;
	private Boolean isNew;
	
	public VehicleDetailDialog() {
		initComponents();
	}

	private void initComponents() {
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();

		setTitle("Edit/Add Vehicle");
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(Color.white);
		setForeground(Color.black);

		setLayout(layout);

		c.gridx = 0;
		c.gridy = 0;
		c.anchor=GridBagConstraints.LINE_END;
		c.ipadx = 16;
		add(yearLabel, c);

		c.gridx = 1;
		c.gridy = 0;
		c.anchor=GridBagConstraints.LINE_START;
		add(yearText, c);

		c.gridx = 0;
		c.gridy = 1;
		c.anchor=GridBagConstraints.LINE_END;
		add(makeLabel, c);

		c.gridx = 1;
		c.gridy = 1;
		c.anchor=GridBagConstraints.LINE_START;
		add(makeText, c);

		c.gridx = 0;
		c.gridy = 2;
		c.anchor=GridBagConstraints.LINE_END;
		add(modelLabel, c);

		c.gridx = 1;
		c.gridy = 2;
		c.anchor=GridBagConstraints.LINE_START;
		add(modelText, c);

		c.gridx = 0;
		c.gridy = 3;
		c.anchor=GridBagConstraints.LINE_END;
		add(seatbeltsLabel, c);

		c.gridx = 1;
		c.gridy = 3;
		c.anchor=GridBagConstraints.LINE_START;
		add(seatbeltsText, c);

		c.gridx = 0;
		c.gridy = 4;
		c.anchor=GridBagConstraints.LINE_END;
		add(licensePlateLabel, c);

		c.gridx = 1;
		c.gridy = 4;
		c.anchor=GridBagConstraints.LINE_START;
		add(licensePlateText, c);

		c.gridx = 0;
		c.gridy = 5;
		c.anchor=GridBagConstraints.LINE_END;
		add(liabPerPersonLabel, c);

		c.gridx = 1;
		c.gridy = 5;
		c.anchor=GridBagConstraints.LINE_START;
		add(liabPerPersonText, c);

		c.gridx = 0;
		c.gridy = 6;
		c.anchor=GridBagConstraints.LINE_END;
		add(liabPerAccidentLabel, c);

		c.gridx = 1;
		c.gridy = 6;
		c.anchor=GridBagConstraints.LINE_START;
		add(liabPerAccidentText, c);
		
		c.gridx = 0;
		c.gridy = 7;
		c.anchor=GridBagConstraints.LINE_END;
		add(propertyDamageLabel, c);

		c.gridx = 1;
		c.gridy = 7;
		c.anchor=GridBagConstraints.LINE_START;
		add(propertyDamageText, c);
		
		c.gridx = 0;
		c.gridy = 8;
		c.anchor=GridBagConstraints.LINE_END;
		add(ownerLabel, c);
		
		c.gridx = 1;
		c.gridy = 8;
		c.anchor=GridBagConstraints.LINE_START;
		add(ownerComboBox, c);

		c.gridx = 0;
		c.gridy = 9;
		c.anchor=GridBagConstraints.LINE_END;
		add(okButton, c);

		c.gridx = 1;
		c.gridy = 9;
		c.anchor=GridBagConstraints.LINE_START;
		add(cancelButton, c);

		setSize(600, 400);
		setModal(true);

		okButton.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent event) {
				okButtonCallback(event);
			}
		});

		cancelButton.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent event) {
				cancelButtonCallback(event);
			}
		});
		
		addWindowFocusListener(new WindowAdapter() {
			public void windowGainedFocus(WindowEvent e) {
				yearText.requestFocusInWindow();
			}
		});
	}
	
	private void okButtonCallback(ActionEvent event) {
		Person owner = (Person)ownerComboBox.getSelectedItem();
		
		Integer year;
		try {
			year = new Integer(yearText.getText());
		} catch (Exception e) {
			year = new Integer("0");
		}
		
		Integer seatbelts;
		try {
			seatbelts = new Integer(seatbeltsText.getText());
		} catch (Exception e) {
			seatbelts = new Integer("0");
		}
		
		Integer liabPerPerson;
		try {
			liabPerPerson = new Integer(liabPerPersonText.getText());
		} catch (Exception e) {
			liabPerPerson = new Integer("0");
		}
		if ((liabPerPerson > 0) && (liabPerPerson < 1000)) {
			liabPerPerson *= 1000;
		}
		
		Integer liabPerAccident;
		try {
			liabPerAccident = new Integer(liabPerAccidentText.getText());
		} catch (Exception e) {
			liabPerAccident = new Integer("0");
		}
		if ((liabPerAccident > 0) && (liabPerAccident < 1000)) {
			liabPerAccident *= 1000;
		}
		
		Integer propertyDamage;
		try {
			propertyDamage = new Integer(propertyDamageText.getText());
		} catch (Exception e) {
			propertyDamage = new Integer("0");
		}
		if ((propertyDamage > 0) && (propertyDamage < 1000)) {
			propertyDamage *= 1000;
		}
		
		Vehicle v = new Vehicle(vehicleId,
				makeText.getText(),
				modelText.getText(),
				year,
				seatbelts,
				liabPerPerson,
				liabPerAccident,
				propertyDamage,
				licensePlateText.getText(),
				owner.getPersonId(),
				owner.getFirstName(),
				owner.getLastName());
		
		
		if (isNew) {
			DataManager.getInstance().addVehicle(v);
		} else {
			DataManager.getInstance().updateVehicle(v);
		}
		
		setVisible(false);
	}
	
	private void cancelButtonCallback(ActionEvent event) {
		setVisible(false);
	}

	public void loadData(Vehicle v) {
		if (v == null) {
			yearText.setText("");
			makeText.setText("");
			modelText.setText("");
			seatbeltsText.setText("");
			licensePlateText.setText("");
			liabPerPersonText.setText("");
			liabPerAccidentText.setText("");
			propertyDamageText.setText("");
			vehicleId = 0;
			isNew = true;
		} else {
			yearText.setText(v.getYear().toString());
			makeText.setText(v.getMake());
			modelText.setText(v.getModel());
			seatbeltsText.setText(v.getSeatbelts().toString());
			licensePlateText.setText(v.getLicensePlate());
			liabPerPersonText.setText(v.getLiabilityPerPerson().toString());
			liabPerAccidentText.setText(v.getLiabilityPerAccident().toString());
			propertyDamageText.setText(v.getPropertyDamage().toString());
			vehicleId = v.getVehicleId();
			isNew = false;
		}
		
		ownerComboBox.setSelectedIndex(-1);
		LinkedList<Person> people = (LinkedList<Person>) DataManager.getInstance().getAllAdults();
		for (Person p : people) {
			ownerComboBox.addItem(p);
			if (v != null) {
				if (p.getPersonId() == v.getOwnerId()) {
					ownerComboBox.setSelectedItem(p);
				}
			}
		}
		
	}

}
